package cn.tedu.sims.controller;


import cn.tedu.sims.pojo.dto.UserManageAddNewDTO;
import cn.tedu.sims.pojo.dto.UserManageDTO;
import cn.tedu.sims.pojo.dto.UserManageUpdateNewDTO;
import cn.tedu.sims.pojo.vo.UserManageListItemVO;
import cn.tedu.sims.service.IUserManageService;
import cn.tedu.sims.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/usermanage")
@Api(tags = "01. 内部用户管理模块")
public class UserManageController {
    @Autowired
    IUserManageService userManageService;

    public UserManageController(){
        log.debug("创建控制器对象：ReportingUserController");
    }

    @ApiOperation("查询用户管理列表")
    @ApiOperationSupport(order = 10)
    @PreAuthorize("hasAuthority('/usermanage/list')")
    @GetMapping("/list")
    public JsonResult<List<UserManageListItemVO>> list() {
        log.debug("开始处理【用户管理查询列表】的业务,无参数");
        List<UserManageListItemVO> list = userManageService.list();
        log.debug("list={}",list);
        return JsonResult.ok(list);
    }
    @ApiOperation("模糊查询用户管理列表")
    @ApiOperationSupport(order = 15)
    @PreAuthorize("hasAuthority('/usermanage/dimList')")
    @PostMapping("/dimList")
    public JsonResult<List<UserManageListItemVO>> dimlist(UserManageDTO userManageDTO){
        log.debug("开始处理【用户管理模糊查询列表】的业务,参数{}",userManageDTO);
        List<UserManageListItemVO> likeList=userManageService.selectUserManage(userManageDTO);
        return JsonResult.ok(likeList);
    }
    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 20)
    @PreAuthorize("hasAuthority('/usermanage/add-new')")
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid UserManageAddNewDTO userManageAddNewDTO){
        log.debug("开始处理【添加用户】的业务,参数:{}",userManageAddNewDTO);
        userManageService.insert(userManageAddNewDTO);
        return JsonResult.ok();
    }
    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 25)
    @PreAuthorize("hasAuthority('/usermanage/id/delete')")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理【删除用户】的业务,参数:{}",id);
        userManageService.deleteByUserId(id);
        return JsonResult.ok();
    }
    @ApiOperation("修改用户")
    @ApiOperationSupport(order = 30)
    @PreAuthorize("hasAuthority('/usermanage/update')")
    @PostMapping("/update")
    public JsonResult<Void> update(@Valid UserManageUpdateNewDTO userManageUpdateNewDTO){
        log.debug("开始处理【修改用户】的业务,参数:{}",userManageUpdateNewDTO);
        userManageService.update(userManageUpdateNewDTO);
        return JsonResult.ok();
    }
}
